package com.ruoyi.web.task;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.web.hb.service.IChannelBuoyService;
import com.ruoyi.web.hbxx.domain.ChannelBuoyInfo;
import com.ruoyi.web.hbxx.domain.ChannelBuoyInfoLog;
import com.ruoyi.web.hbxx.service.IChannelBuoyInfoLogService;
import com.ruoyi.web.hbxx.service.IChannelBuoyInfoService;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("channelBuoyTask")
public class ChannelBuoyTask {
    private static final Logger log = LoggerFactory.getLogger(ChannelBuoyTask.class);
    @Autowired
    private IChannelBuoyService channelBuoyService;
    @Autowired
    private IChannelBuoyInfoLogService channelBuoyInfoLogService;
    @Autowired
    private IChannelBuoyInfoService channelBuoyInfoService;

    public void getNavigationList() throws IOException {
        log.info("==========同步南方测绘航标任务开始==========");
        List<Map> navigationList = channelBuoyService.getNavigationList();
        if (navigationList == null) {
             navigationList = channelBuoyService.getNavigationList();
        }
        ModelMapper modelMapper = new ModelMapper();

        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

        //航标数据
        List<ChannelBuoyInfo> channelBuoyInfoList = navigationList.stream()
                .map(map -> modelMapper.map(map, ChannelBuoyInfo.class)).collect(Collectors.toList());
        //航标日志
        List<ChannelBuoyInfoLog> channelBuoyInfoLogList = navigationList.stream()
                .map(map -> modelMapper.map(map, ChannelBuoyInfoLog.class)).collect(Collectors.toList());
        for (ChannelBuoyInfo channelBuoyInfo : channelBuoyInfoList) {
            channelBuoyInfo.setId(null);
            int i = channelBuoyInfoService.updateChannelBuoyInfo(channelBuoyInfo);
            if (i == 0) {
                channelBuoyInfoService.insertChannelBuoyInfo(channelBuoyInfo);
            }
        }
        for (ChannelBuoyInfoLog channelBuoyInfoLog : channelBuoyInfoLogList) {
            channelBuoyInfoLog.setId(null);
            channelBuoyInfoLogService.insertChannelBuoyInfoLog(channelBuoyInfoLog);
        }
        log.info("==========同步南方测绘航标任务结束==========");

    }
}
